<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

// 获取pc端栏目模板
function column_tpl() {
    $tpl = [];
    foreach (glob(Env('root_path') . 'public/template/pc/*.html') as $v) {
        $tpl[] = [
            'value' => str_replace('.html', '', basename($v)),
            'title' => basename($v),
        ];
    }
    return $tpl;
}

// 栏目树
function column_tree($field = '*', $notin = []) {
    $column = db('column')->field($field)->where('id', 'not in', $notin)->order('sort,id')->select();

    $key_by_id = [];
    foreach ($column as $k => $v) {
        $column[$k]['spread'] = true;   // 用户树形菜单展开
        $key_by_id[$v['id']] = &$column[$k];
    }

    foreach ($column as $k => $v) {
        if (array_key_exists($v['pid'], $key_by_id)) {
            $key_by_id[$v['pid']]['children'][] = &$column[$k];
        }
    }

    foreach ($column as $k => $v) {
        if ($v['pid'] > 0) {
            unset($column[$k]);
        }
    }

    return array_values($column);
}

// 栏目列表，带level（栈实现）
function column_list($field = '*', $notin = []) {
    $column = db('column')->field($field)->where('id', 'not in', $notin)->order('sort desc,id desc')->select();

    // 顶级栏目入栈
    $stack = [];
    foreach ($column as $k => $v) {
        if (0 == $v['pid']) {
            $v['level'] = 0;
            $stack[] = $v;
            unset($column[$k]);
        }
    }
    
    $list = [];
    while (count($stack)) {
        // 出栈
        $currentColumn = array_pop($stack);
        // 是否有子栏目
        $currentColumn['children'] = false;

        foreach($column as $k => $v) {
            // 出栈栏目是否有子栏目，是所有子栏目入栈
            if ($v['pid'] == $currentColumn['id']) {
                $v['level'] = $currentColumn['level'] + 1;
                $stack[] = $v;
                unset($column[$k]);

                $currentColumn['children'] = true;
            }
        }

        // 赋值
        $list[] = $currentColumn;
    }

    return $list;
}

// 栏目子节点ID，包括自身节点
function column_childrens_id($column_id = 0) {
    $childrens_id = [];

    $column = db('column')->column('pid', 'id');
    if (array_key_exists($column_id, $column)) {
        $childrens_id[] = $column_id;
        unset($column[$column_id]);
        do {
            $tag = false;
            foreach ($column as $id => $pid) {
                if (in_array($pid, $childrens_id)) {
                    $childrens_id[] = $id;
                    unset($column[$id]);
                    $tag = true;
                }
            }
        } while ($tag);
    }

    return $childrens_id;
}

// 栏目父节点ID，包括自身节点
function column_parents_id($column_id = 0) {
    $parents_id = [];

    $column = db('column')->column('pid', 'id');
    while (array_key_exists($column_id, $column)) {
        $parents_id[] = $column_id;
        $column_id = intval($column[$column_id]);
    }

    return array_reverse($parents_id);
}

// 获取当前位置
function get_pos($column_id = 0) {
    $pos = [];

    $column = cache('column');
    while (array_key_exists($column_id, cache('column'))) {
        $url = '<a href="/column/' . cache('column')[$column_id]['id'] . '.html">' . cache('column')[$column_id]['name'] . '</a>';
        $pos[] = $url;
        $column_id = cache('column')[$column_id]['pid'];
    }

    return implode(' > ', array_reverse($pos));
}

// 获取编辑器
function get_editor($editor = '') {
    $script = '';
    if ($editor == 'ueditorplus') {
  
    } else if ($editor == 'niceditor') {
        
    } else if ($editor == 'jodit') {

    }
}